<?php namespace App\Http\Controllers\Backstage;
/**
 * 后台权限管理
 *
 * 后台权限管理controller
 *
 * @author    zhouchao <zhouchao@yunys.com.cn>
 * @copyright 2016 QuanZhouYunYiShang Inc.
 */
use App\Http\Controllers\Controller;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\Permission;
use App\Services\PermissionsService\PermissionsService;
use App\Services\Presenters\MenuPresenter;

class PermissionsController extends Controller
{

    public function __construct(PermissionsService $service,MenuPresenter $menu)
    {
        $this->service = $service;
        $this->menu = $menu;
    }

    /**
     * 获取权限列表
     * @return $this
     */
    public function getList(Request $request){
        //$menus = $this->service->getPermissionParent();
        //$menus = $this->menu->sidebarMenus($menus);
        //$this->menu->getSidebarMenus();
        $display_name = $request->input('display_name') === null ? '' : trim($request->input('display_name'));
        //dd($display_name);
        $permissions = $this->service->getPermissionList(array('per_page'=>5,'display_name'=>$display_name));
        //dd($permissions->toArray());
        return view('backstage.permissions.list')
            ->with('permissions',$permissions)
            ->with('display_name',$display_name);
    }

    /**
     * 添加权限
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function getAdd(){
        $permissions = $this->service->getPermissionParent();
        return view('backstage.permissions.add')
            ->with('permissions',$permissions);
    }

    /**
     * 添加权限
     * @param Request $request
     * @return \Illuminate\Http\RedirectResponse
     */
    public function postAdd(Request $request){
        $this->validate($request,[
            'name' => 'required|max:200|unique:permissions,name',
            'display_name' => 'required|max:200|unique:permissions,display_name',
            'description' => 'max:200',
            'parent_id' => 'required|integer',
            'is_menu' => 'required|boolean'
        ]);
        $name = trim($request->input('name'));
        $display_name = trim($request->input('display_name'));
        $description = trim($request->input('description'));
        $parent_id = $request->input('parent_id');
        $is_menu = $request->input('is_menu');
        $permission = new Permission;
        $permission->name = $name;
        $permission->display_name = $display_name;
        $permission->description = $description;
        $permission->parent_id = $parent_id;
        $permission->is_menu = $is_menu;
        $permission->save();
        return redirect()->back()->with('success_message','添加成功');
    }

    /**
     * 更新页面
     * @param $id
     * @return $this
     */
    public function getUpdate($id){
        $permission = Permission::find(intval($id));
        $permissions = $this->service->getPermissionParent();
        return view('backstage.permissions.update')
            ->with('permission',$permission)
            ->with('permissions',$permissions);
    }

    /**
     * 更新
     * @param Request $request
     * @return \Illuminate\Http\RedirectResponse
     */
    public function postUpdate(Request $request){
        $this->validate($request,[
            'id' => 'required|integer',
            'name' => 'required|max:200|unique:permissions,name,'.intval($request->input('id')).',id',
            'display_name' => 'required|max:200|unique:permissions,display_name,'.intval($request->input('id')).',id',
            'description' => 'max:200',
            'parent_id' => 'required|integer',
            'is_menu' => 'required|boolean'
        ]);
        $name = trim($request->input('name'));
        $display_name = trim($request->input('display_name'));
        $description = trim($request->input('description'));
        $parent_id = $request->input('parent_id');
        $is_menu = $request->input('is_menu');
        $permission = Permission::find(intval($request->input('id')));
        $permission->name = $name;
        $permission->display_name = $display_name;
        $permission->description = $description;
        $permission->parent_id = $parent_id;
        $permission->is_menu = $is_menu;
        $permission->save();
        return redirect('backstage/permissions/list')->with('success_message','更新成功');
    }

    /**
     * 删除权限
     * @param $id
     * @return \Illuminate\Http\RedirectResponse
     */
    public function getDelete($id){
        $child_permissions = Permission::where('parent_id',$id)->get()->toArray();
        if(count($child_permissions) > 0){
            return redirect()->back()->with('error_message','该权限下还有子权限，不能删除');
        }
        Permission::where('id', intval($id))->delete();
        //删除权限
        DB::table('permission_role')->where('permission_id','=',intval($id))->delete();
        return redirect()->back()->with('success_message','删除成功');
    }

    /**
     * 没有权限页面
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function getNoPermissions(){
        return view('backstage.compe');
    }
}
